//
//  UIViewController+ButtonItem.m
//  AmberWeather
//
//  Created by LeeSongtao on 16/3/9.
//  Copyright © 2016年 Amber Weather. All rights reserved.
//

#import "UIViewController+ButtonItem.h"

@implementation UIViewController (ButtonItem)

#pragma mark createLeftBarButtonItem

- (void)createLeftBarButtonItemWithTitle:(NSString *)title
{
    CGRect frame = CGRectMake(0, 0, 40, 40);
    [self createLeftBarButtonItemWithFrame:frame title:title];
}

- (void)createLeftBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title
{
    NSString *selectorStr = @"clickLeftNavigationItemButton";
    self.navigationItem.leftBarButtonItem = [self createBarButtonItemWithFrame:frame title:title selectorStr:selectorStr image:nil];
}

- (void)createLeftBarButtonItemWithFrame:(CGRect)frame image:(NSString *)imageName
{
    NSString *selectorStr = @"clickLeftNavigationItemButton";
    self.navigationItem.leftBarButtonItem = [self createBarButtonItemWithFrame:frame title:@"" selectorStr:selectorStr image:imageName];
}

- (UIBarButtonItem *)createBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title selectorStr:(NSString *)selectorStr
{
    return [self createBarButtonItemWithFrame:frame title:title selectorStr:selectorStr image:nil];
}

/**
 *  创建一个UIBarButtonItem
 */
- (UIBarButtonItem *)createBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title selectorStr:(NSString *)selectorStr image:(NSString *)imageName
{
    UIButton * button = [UIButton createButton];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    if (imageName.length) {
//        [button setBackgroundImage:imageNamed(imageName) forState:UIControlStateNormal];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//        [button setImage:[iImageNamed(imageName) imageByApplyingAlpha:AWHighLightAlpha] forState:UIControlStateHighlighted];
    }
//    button.backgroundColor = [UIColor orangeColor];
    if (selectorStr.length) {
        SEL selector = NSSelectorFromString(selectorStr);
        [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    }
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return leftItem;
}

- (void) clickLeftNavigationItemButton{NSLog(@"clickLeftNavigationItemButton");}


#pragma mark createLabelLeftBarButtonItem

- (void)createLabelLeftBarButtonItemWithTitle:(NSString *)title
{
    self.navigationItem.leftBarButtonItem = [self createLabelBarButtonItemWithFrame:CGRectZero title:title];
}

- (void)createLabelLeftBarButtonItemWithTitle:(NSString *)title font:(UIFont *)font
{
    self.navigationItem.leftBarButtonItem = [self createLabelBarButtonItemWithFrame:CGRectZero title:title font:font textColor:nil];
}

- (UIBarButtonItem *)createLabelBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title
{
   return [self createLabelBarButtonItemWithFrame:frame title:title font:nil];
}

- (UIBarButtonItem *)createLabelBarButtonItemWithTitle:(NSString *)title font:(UIFont *)font
{
    return [self createLabelBarButtonItemWithFrame:CGRectZero title:title font:font textColor:nil];
}

- (UIBarButtonItem *)createLabelBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font
{
   return [self createLabelBarButtonItemWithFrame:frame title:title font:font textColor:nil];
}

- (UIBarButtonItem *)createLabelBarButtonItemWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font textColor:(UIColor *)textColor
{
//    if (CGSizeEqualToSize(frame.size, CGSizeZero)) {
//        CGSize size = [SampuxGlobalTool getSizeWithString:title andFont:font andMaxSize:CGSizeMake(150, 30)];
//        frame = CGRectMake(0, 0, size.width, size.height);
//    }
    UILabel *label = [UILabel createLabelWithFrame:frame textAlignment:NSTextAlignmentLeft textColor:textColor font:font];
    label.textColor = THSystemWhiteColor;
    label.text = title;
//    label.backgroundColor = AWBackgroundColor;
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:label];
    return item;
}

- (void)createBackBarButtonItem
{
    self.navigationItem.leftBarButtonItem = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) title:@"" selectorStr:@"popSelfViewController" image:@"arrow_alpha"];
    
}
- (void)createAnotherBackBarButtonItem
{
    self.navigationItem.leftBarButtonItem = [self createBarButtonItemWithFrame:CGRectMake(10, 10, 20, 20) title:@"" selectorStr:@"popSelfViewController" image:@"back2"];
}

- (void)createRightBackBarButtonItem
{
    self.navigationItem.rightBarButtonItem = [self createBarButtonItemWithFrame:CGRectMake(10, 10, 20, 20) title:@"" selectorStr:@"popSelfViewController" image:@"home-white"];
}

- (void)popSelfViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
